import { CollectionUtils } from "@chulei-ui/utils";
import type { UniverseRegion } from "./UniverseRegion";
import type { UniverseSystem } from "./UniverseSystem";
import type ExcelJs from "@scvzerng/exceljs";
export class UniverseConstellation {
  // 星座ID
  constellation_id!: number;

  // 此星座所在的区域
  region?: UniverseRegion;
  // 名称
  name!: string;

  // 星系
  systems: UniverseSystem[] = [];

  static fromConstellation(input: any) {
    const universeConstellation = new UniverseConstellation();
    universeConstellation.constellation_id = input.constellation_id;
    universeConstellation.name = input.name;
    return universeConstellation;
  }

  static fromSheet(sheet: ExcelJs.Worksheet, regions: UniverseRegion[]) {
    const regionMap = CollectionUtils.array2Map<UniverseRegion>(regions, (region: UniverseRegion) => region.region_id);
    const constellations: UniverseConstellation[] = [];
    sheet.eachRow((row, index) => {
      if (index > 1) {
        const id = row.getCell(1).text!;
        const name = row.getCell(2).text!;
        const regionId = row.getCell(3).text!;
        const constellation = new UniverseConstellation();

        const region = regionMap[regionId];
        constellation.constellation_id = Number(id);
        constellation.name = name;
        constellation.region = region;
        region.constellations.push(constellation);
        constellations.push(constellation);
      }
    });
    return constellations;
  }
}
